﻿using System;
using Microsoft.AspNetCore.Mvc;
using Crossroad.Core;
using Crossroad.System.Api;
using Crossroad.Core.Models;
using Newtonsoft.Json.Linq;
using Crossroad.Utils.Extensions;
using Crossroad.OAuth.Attributes;
using Microsoft.AspNetCore.Hosting;

namespace Crossroad.System.Common.Controller
{

    /// <inheritdoc />
    /// <summary>
    /// 上传
    /// </summary>
    [ApiAuthorize]
    [Route("api/file/upload")]
    public class UploadController : BaseApi
    {

        private readonly IHostingEnvironment hostingEnvironment;

        public UploadController(
            IHostingEnvironment hostingEnvironmentIpt
        )
        {
            hostingEnvironment = hostingEnvironmentIpt;
        }

        /// <summary>
        /// 图片批量上传
        /// </summary>
        [HttpPost("img/batch")]
        public void ImgBatch(
        )
        {
        }

        /// <summary>
        /// 单个图片上传 formData 方式
        /// </summary>
        /// <returns></returns>
        [HttpPost("image/single")]
        public object ImageSingle()
        {
            var file = HttpUtil.UploadImage($@"{hostingEnvironment.WebRootPath}\",Request.HttpContext).Result;

            return new ResDicData
            {
                Result = new
                {
                    file.id,
                    file.name,
                    path = $"/{file.relativePath}",
                    url = $"{Request.Protocol.Substring(0, Request.Protocol.IndexOf('/')).ToLower()}://{Request.Host}/{file.relativePath}"
                }
            };
        }

        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPost("image/base64")]
        public object imageBase64(
            [FromBody] JObject parameter
        )
        {
            var m = new
            {
                image = parameter.Get<string>("image"),
                name = parameter.Get<string>("name"),
            };
            var file = HttpUtil.SaveImage($@"{hostingEnvironment.WebRootPath}\", m.image, m.name);

            return new ImageDom
            {
                id = file.id,
                name = file.name,
                path = $"/{file.relativePath}",
                url = $"{Request.Protocol.Substring(0, Request.Protocol.IndexOf('/')).ToLower()}://{Request.Host}/{file.relativePath}"
            };
        }

        /// <summary>
        /// 上传单个文件
        /// </summary>
        /// <returns></returns>
        [HttpPost("single")]
        public object File()
        {
            var file = HttpUtil.UploadFile($@"{hostingEnvironment.WebRootPath}\",Request.HttpContext).Result;

            return new ResDicData
            {
                Result = new
                {
                    file.id,
                    file.name,
                    path = $"/{file.relativePath}",
                    url = $"{Request.Protocol.Substring(0, Request.Protocol.IndexOf('/')).ToLower()}://{Request.Host}/{file.relativePath}"
                }
            };
        }


    }
}
